import { createRouter, createWebHistory } from 'vue-router'

export const menu_router = [
  {
    path: '/',
    name: 'home',
    meta: {
      title: '首页'
    },
    component: () =>import('../views/home/indexView.vue')
  },
  {
    path: '/set',
    meta: {
      title: '设置',
      roles: ['admin']
    },
    component: () => import('../views/set/indexView.vue')
  },
  {
    path: '/ziluyou',
    meta: {
      title: '列表管理'
    },
    children: [
      {
        path: '/ziluyou/list',
        meta: {
          title: '列表'
        },
        component: () => import('../views/list/indexView.vue')
      },
      {
        path: '/ziluyou/listadd',
        meta: {
          title: '新增',
          roles: ['admin']
        },
        component: () => import('../views/listadd/indexView.vue')
      },
      {
        path: '/ziluyou/listedit',
        meta: {
          title: '编辑',
          roles: ['admin']
        },
        component: () => import('../views/listedit/indexView.vue')
      },
    ]
  },
  {
    path: '/customer',
    meta: {
      title: '客服',
    },
    component: () => import('../views/customer/indexView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: () => import('../views/layout/indexView.vue'),
      children: menu_router
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/indexView.vue')
    },
    {
      path: '/403',
      name: '403',
      component: () => import('../views/403/indexView.vue')
    }
  ]
})

router.beforeEach((to, form, next)=>{
  // 获取本地存储的role值
  const role = localStorage.getItem('role')
  // 判断，如果路由里面有roles数组，但是数组里面不包含本地存储的值，就跳转403页面，表示没有权限
  if(to.meta.roles && !to.meta.roles.includes(role)){
    next({path: '/403'})
  }
  next()
})

export default router
